package com.gxa.eshop.portal.controller;



import com.github.pagehelper.PageInfo;
import com.gxa.eshop.core.pojo.User;
import com.gxa.eshop.core.utils.JsonResult;
import com.gxa.eshop.portal.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;
import java.util.Date;

@RestController
@RequestMapping("/user")
@Validated
@Api("用户操作接口")
public class UserController {
    @Resource
    UserService userService;

    @ApiOperation("通过id获取用户对象的方法")
    @ApiImplicitParams(
            @ApiImplicitParam(name="id",value = "用户的编号",paramType = "query")
    )
    @PostMapping("/get")
    public JsonResult get(@RequestParam("id") Integer id) {
        User user = userService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(user);
        return jsonResult;
    }

    @ApiOperation("添加用户的方法")
    @ApiImplicitParams(
            @ApiImplicitParam(name="user",value = "用户对象",paramType = "body")
    )
    @PostMapping("/add")
    public JsonResult getAdd(@Validated User user) {
         userService.add(user);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(user);
        return jsonResult;
    }
    @PostMapping("/selectBypage")
    public JsonResult selectByPage(@Min(value = 1) @RequestParam("page") Integer page, @RequestParam("size") Integer size){
        PageInfo<User> userPageInfo = userService.selectByPage(page, size);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("分页查询成功");
        jsonResult.setData(userPageInfo);
        return jsonResult;
    }
}
